package com.example.gzysmartcity.ui.fragment.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.gzysmartcity.databinding.FragmentPersonalityBinding;
import com.example.gzysmartcity.ui.base.BaseFragment;

import org.jetbrains.annotations.NotNull;

public class PersonalityFragemnt extends BaseFragment<FragmentPersonalityBinding> {
    SharedPreferences sp;
    @Override
    protected FragmentPersonalityBinding viewBinding() {
        return FragmentPersonalityBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        sp= requireActivity().getSharedPreferences("me", Context.MODE_PRIVATE);
        String sperson=sp.getString("personality","");
        if (sperson!=null){
            b.editYjfankui.setText(sperson);
        }
        b.editYjfankui.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                b.textIndex.setText(s.length()+"/150");
                if (s.length()>150){
                    b.editYjfankui.setText(s.toString().substring(0,150));
                    Toast.makeText(getContext(), "最多输入150字", Toast.LENGTH_SHORT).show();
                }
            }
        });
        b.tvPsabaocun.setOnClickListener(v->{
            SharedPreferences.Editor editor=sp.edit();
            editor.putString("personality",b.editYjfankui.getText().toString());
            editor.apply();
            Toast.makeText(getContext(), "保存成功", Toast.LENGTH_SHORT).show();
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        show("f2");
    }

    @Override
    protected String setTitle() {
        return "个性签名";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }
}
